﻿using Model;
using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class RoleService
    {
        public static List<Role> GetRoles()
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {

                return db.Role.ToList();
            }
        }
        /// <summary>
        /// 角色数据
        /// </summary>
        /// <param name="limit"></param>
        /// <param name="page"></param>
        /// <param name="Name"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public static List<Role> GetRoles(int limit, int page, string Name, out int total)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                var query = db.Role.Where(p => true);
                if (!string.IsNullOrEmpty(Name))
                {
                    query = query.Where(p => p.RoleName.Contains(Name));
                }
                total = query.Count();
                return query.OrderBy(p => p.Id).Skip((page - 1) * limit).Take(limit).ToList();
            }
        }

        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public static int GetAdd(Role role)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                db.Role.Add(role);
                return db.SaveChanges();
            }
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public static int GetBatchDelByIds(string Ids)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                string newIds = Ids.Substring(0, Ids.Length - 1);
                string sql = $"delete [Role] where Id in ({newIds})";
                return db.Database.ExecuteSqlCommand(sql);
            }
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static int GetDel(int id)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                Role role = new Role() { Id = id };
                db.Role.Attach(role);
                db.Role.Remove(role);
                return db.SaveChanges();
            }
        }
        /// <summary>
        /// 根据角色id查找角色数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Role GetSelect(int id)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {

                return db.Role.FirstOrDefault(p=>p.Id==id);
            }
        }
        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public static int GetUpdate(Role role)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                db.Role.AddOrUpdate(role);
                return db.SaveChanges();
            }
        }
    }
}
